import Vue from 'vue'
import VueX from 'vuex'

Vue.use(VueX)
const state = { count: 100 }

const actions = {
  increment({ commit }, num) {
    console.log(commit)
    commit('INCREMENT', num)
  },
  decrement({ commit }, num) {
    commit('DECREMENT', num)
  },
  incrementIffOdd({ commit }, num) {
    commit('INCREMENTIFFODD', num)
  },
  incrementAsync({ commit }, num) {
    setTimeout(() => {
      commit('INCREMENTASYNC', num)
    }, 1000)
  },
}

const mutations = {
  INCREMENT(state, num) {
    state.count += num
  },
  DECREMENT(state, num) {
    state.count -= num
  },
  INCREMENTIFFODD(state, num) {
    if (state.count & 1) {
      state.count += num
    }
  },
  INCREMENTASYNC(state, num) {
    state.count += num
  },
}

const store = new VueX.Store({
  state,
  actions,
  mutations,
})

export default store
